<template>
  <div class="consultation">
    <h2>用户管理</h2>
    <div class="flex-center">
      <el-button class="ml0" size="mini" type="success" @click="addUser"
        >新增用户</el-button
      >
      <el-input v-model="key" size="mini" style="width: 120px;" placeholder="关键字/词查询"></el-input>
      <el-button class="ml0" size="mini" type="primary" @click="refreshTable(true)"
        >查询</el-button
      >
    </div>
    <div class="consultation-block">
      <div class="table">
        <el-table size="mini" :data="tableData" style="width: 100%">
          <el-table-column label="序号" type="index"> </el-table-column>
          <el-table-column prop="phone" label="用户名"> </el-table-column>
          <el-table-column prop="nickname" label="昵称"> </el-table-column>
          <el-table-column prop="role" label="角色"> </el-table-column>
          <el-table-column label="操作" width="250">
            <template slot-scope="scope">
              <el-button
                type="warning"
                size="mini"
                @click="updateUser(scope.row)"
                >编辑</el-button
              >
              <el-button
                type="danger"
                size="mini"
                @click="deleteUser(scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </div>

      <div class="pagination">
        <el-pagination
          background
          layout="prev, pager, next"
          :current-page.sync="currentPage"
          @current-change="refreshTable"
          :page-size="pageSize"
          :total="total"
        >
        </el-pagination>
      </div>
    </div>
    <el-dialog
      :title="addForm.id ? '编辑' : '新增'"
      :visible.sync="isAddVisible"
      :close-on-click-modal="false"
      width="30%"
      :before-close="handleAddClose"
    >
      <el-form
        :model="addForm"
        status-icon
        :rules="rules2"
        size="mini"
        ref="addForm"
        label-width="100px"
      >
        <el-form-item label="用户名" prop="phone">
          <el-input :min="0" type="text" v-model="addForm.phone"></el-input>
        </el-form-item>
        <el-form-item label="昵称">
          <el-input type="text" v-model="addForm.nickname"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="text" v-model="addForm.password"></el-input>
        </el-form-item>
        <el-form-item label="角色" prop="role">
          <el-radio-group v-model="addForm.role">
            <el-radio label="管理员">管理员</el-radio>
            <el-radio label="学生">学生</el-radio>
            <el-radio label="教师">教师</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="isAddVisible = false">取 消</el-button>
        <el-button
          type="primary"
          :loading="btnLoading"
          @click="submitForm('addForm')"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { addUser, getUser, deleteUser, updateUser, getPaper } from "../service.js";

export default {
  name: "home",
  mounted() {
    this.refreshTable();
  },
  data() {
    return {
      tableData: [],
      currentPage: 1,
      pageSize: 10,
      total: 0,
      isVisible: false,
      isAddVisible: false,
      dialogTitle: "",
      btnLoading: false,
      addForm: {
        phone: "",
        role: "管理员",
        username: "",
        surplus: "",
      },
      rules2: {
        phone: [{ required: true, message: '用户名不能为空', trigger: "blur" }],
        password: [{ required: true, message: '密码不能为空', trigger: "blur" }],
      },
      key: ''
    };
  },
  methods: {
    handleAddClose() {
      this.isAddVisible = false;
    },
    submitForm(formName) {
      this.btnLoading = true;
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.addForm.id) {
            let params = JSON.parse(JSON.stringify(this.addForm))
            updateUser(params.id, params).then((res) => {
              if (res.code == 200) {
                this.$message.success("修改成功");
                this.isAddVisible = false;
                this.refreshTable();
              } else {
                this.$message.error(res.msg[0].msg);
              }
              this.btnLoading = false;
            });
          } else {
            let params = JSON.parse(JSON.stringify(this.addForm))
            addUser({
              ...params
            }).then((res) => {
              if (res.code == 200) {
                this.$message.success("添加成功");
                this.isAddVisible = false;
                this.refreshTable();
              } else {
                this.$message.error(res.msg[0].msg);
              }
              this.btnLoading = false;
            });
          }
        }
      });
    },
    refreshTable(init = false) {
      if (init == "init") {
        this.currentPage = 1;
      }
      let params = {
        pageSize: this.pageSize,
        page: this.currentPage,
      };
      if (this.key) {
        params.key = this.key
      }
      getUser(params).then((res) => {
        this.tableData = res.code === 200 ? res.data.data : [];
        this.total = res.data.total;
      });
    },
    addUser() {
      this.addForm = {
        phone: "",
        nickname: "",
        password: "",
        role: "管理员",
      }
      this.isAddVisible = true;
      this.$nextTick(() => {
        this.$refs["addForm"].resetFields();
      });
    },
    deleteUser(row) {
      this.$confirm("此操作将永久删除该记录, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        deleteUser(row.id).then((res) => {
          if (res.code == 200) {
            this.$message.success("删除成功");
            this.isVisible = false;
            this.refreshTable();
          } else {
            this.$message.error(res.msg);
          }
        });
      });
    },
    updateUser(row) {
      this.addForm = JSON.parse(JSON.stringify(row))
      this.isAddVisible = true
    },
  },
};
</script>

<style lang="scss" scoped>
h2 {
  text-align: center;
  margin-top: 20px;
  margin-bottom: 20px;
}
.consultation-block {
  width: 90%;
  margin-left: 5%;
}
.el-tag + .el-tag {
  margin-left: 10px;
}
.button-new-tag {
  margin-left: 10px;
  height: 32px;
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;
}
.input-new-tag {
  width: 90px;
  margin-left: 10px;
  vertical-align: bottom;
}
.edit {
  color: #409EFF;
  cursor: pointer;
}
.delete {
  color: #F56C6C;
  cursor: pointer;
}
.charts {
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 20px;

  .chart {
    width: 100%;
    height: 200px;
  }
}
</style>
